package praktikumid.w11.p3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class UndoWithHistory {
	/**
	 * Simple element, which technically only has one String - name.
	 * 
	 * @author Ago
	 * 
	 */
	static class Element {
		/**
		 * Name of the element.
		 */
		String name;

		/**
		 * Constructor, where we set the name.
		 * 
		 * @param name
		 */
		public Element(String name) {
			this.name = name;
		}

		public Element(Element copy) {
			name = copy.name;
		}

		@Override
		public String toString() {
			// let's have a "nice" printout
			return name;
		}
	}

	/**
	 * A list of elements.
	 */
	static List<Element> elements = new ArrayList<Element>();
	
	static List<List<Element>> history = new ArrayList<List<Element>>();
	static int historyIndex = -1;

	/**
	 * Main.
	 * 
	 * @param args
	 *            arguments.
	 * @throws IOException
	 *             Dealing with input can throw exception.
	 */
	public static void main(String[] args) throws IOException {
		newState();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			printHelp();
			String line = br.readLine();
			// we expect correct input
			// just split everything by space
			String[] tokens = line.split(" ");
			int op = Integer.valueOf(tokens[0]);
			int elid = -1;
			switch (op) {
			case 1: // add element
				elements.add(new Element(tokens[1]));
				break;
			case 2: // modify element, rename
				elid = Integer.valueOf(tokens[1]);
				Element e = elements.get(elid);
				e.name = tokens[2];
				break;
			case 3: // delete element
				elid = Integer.valueOf(tokens[1]);
				elements.remove(elid);
				break;
			case 4: // printout
				printElements();
				break;
			case 5: // undo
				undo();
				printElements();
				break;
			case 0:
				System.exit(0);
			}
			if (op > 0 && op < 4) {
				newState();
			}

		}

	}

	public static void copyCurrentState() {
		List<Element> newElements = new ArrayList<Element>();
		for (Element e : elements) {
			newElements.add(new Element(e));
			// Element e2 = new Element();
			// e2.name = e.name;
			// newElement.add(e2);
		}
		elements = newElements;
	}
	
	public static void newState() {
		// copy current state
		// add to list
		historyIndex++;
		while (historyIndex < history.size()) {
			history.remove(historyIndex);
		}
		history.add(elements);
		copyCurrentState();
	}
	
	public static void undo() {
		if (historyIndex > 0) {
			historyIndex--;
			elements = history.get(historyIndex);
			copyCurrentState();
		}
	}
	/**
	 * Prints out the elements in the list.
	 */
	public static void printElements() {
		System.out.println("Elements:");
		for (int i = 0; i < elements.size(); i++) {
			System.out.println(i + ". " + elements.get(i));
		}
	}

	/**
	 * Prints out help - what can be done with the program.
	 */
	public static void printHelp() {
		System.out.println("  1 add element [1 name]");
		System.out.println("  2 change element [2 elid name]");
		System.out.println("  3 delete element [3 elid]");
		System.out.println("  4 list elements [4]");
		System.out.println("  5 undo [5]");
		System.out.println("  0 exit [0]");
	}

}
